package com.klun.project.common.constans.cpmodel;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CpModel {
	ExecutorService executorService = Executors.newFixedThreadPool(2);

	public CpModel() {
	}

	private static volatile int count = 0;
	private final String LOCK = "lock";

	public void service() {
		executorService.submit(() -> {
			do {
				synchronized (LOCK) {
					if (count < 10) {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						count++;
						System.out.println("生产一个,当前" + count);
					} else {
						System.out.println("产能过剩休息一次");
					}
				}
			} while (true);

		});
		executorService.submit(() -> {
			do {
				synchronized (LOCK) {
					if (count > 0) {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						count--;
						System.out.println("消费一个,当前" + count);
					} else {
						System.out.println("需求旺盛,催动生产" + count);
					}
				}
			} while (true);
		});
	}

	public static void main(String[] args) {
		CpModel cp = new CpModel();
		cp.service();
	}


}
